<?php
namespace Summer\Hjd\Models;

use \Backend\Models\ExportModel;
use Input;

class DetailedLineExport extends ExportModel {
    protected $fillable = [
        'start_date',
        'end_date',
    ];
    public function exportData($columns, $sessionKey = null)
    {
        if(Input::has('ExportOptions')){
            $filter=Input::get('ExportOptions');
            $start_date=$filter['start_date'];
            $end_date=$filter['end_date'];
        }
        $query=DetailedLineModel::select();
        if($start_date){
            $query->where('created_at','>',$start_date);
        }
        if($end_date){
            $query->where('created_at','<',$end_date);
        }
        $records = $query->get();
        $records->each(function($record) use ($columns) {
            $record->target_username=$record->target->username??'暂无';
            $record->target_userphone=$record->target->userphone??'暂无';
            $record->relate_username=$record->relate->username??'暂无';
            $record->relate_userphone=$record->relate->userphone??'暂无';
            if($record->inout==1){
                $record->money="+".$record->money;
            }else{
                $record->money="-".$record->money;
            }
            $record->change_title=$record->change->title;
            $record->addVisible($columns);
        });
        return $records->toArray();
    }
}
